import cTitle from "components/title";
import { Toast } from 'vant';

export default {
  data() {
    return {
      address: "",
      showContent: false,
      carousels: [],
      projects: [],
      workers: [],

      point: {},
      city: '',
    };
  },
  activated() {
    this.ready();
  },
  computed: {
    appointmentLang() {
      if(this.$store.state.shopLanguage.appointment) {
        return this.$store.state.shopLanguage.appointment;
      }else {
        return {worker: "技师", project: "项目", service: "服务"};
      }
    },
  },
  methods: {
    toUrl(str, params) {
      this.$router.push(this.fun.getUrl(str, params));
    },
    getData() {
      $http
        .post(
          "plugin.appointment.frontend.index.get-data",
          {
            lng: this.point.lng,
            lat: this.point.lat,
            city_name: this.city
          },
          "获取中"
        )
        .then(response => {
          if (response.result === 1) {
            this.carousels = response.data.carousels;
            this.projects = response.data.projects;
            this.workers = response.data.workers;
          } else {
            this.$dialog.alert({message:response.msg});

          }
          this.showContent = true;
        })
        .catch(err => {
          console.log(err);
        });
    },
    tolocation() {
      this.$router.push(this.fun.getUrl("o2oLocation", {}, {tag:"appointment"}));
    },
    tosearch() {
      if (!this.city) {
        Toast({
          message: "请先选择城市"
        });
        return;
      }
      this.$router.push(
        this.fun.getUrl("AppointmentSearch", {
          city: this.city,
          point: JSON.stringify(this.point)
        })
      );
    },
    //banner点击
    bannerClick(item) {
      if (!this.fun.isTextEmpty(item.link)) {
        window.location.href = item.link; //跳转
      }
    },
    ready() {
      let myLocation = this.$store.state.o2oLocation;

      if (myLocation.point) {
        this.address = myLocation.title;
        this.city = myLocation.city;
        this.point = myLocation.point;
        //初始化数据
        this.getData();
      } else {
        // 防止首页空白定位前先请求一下接口
        this.getData();
        console.log("location不存在");
        this.fun.getLocation()
          .then((res)=> {
            this.point = res.point;
            this.address = res.title;
            this.city = res.city;
            this.getData();
          })
          .catch((err)=> {
            console.log(err);
            this.getData();
          });
      }
    },
  },
  components: { cTitle }
};
